/**
 *  test_irq.c - Test codes for interruption management and its functions.
 *               It runs on Windows or Linux.
 *
 *  Copyright (C) 2008  ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include "test_irq.h"

#include "irq_stubs.h"
#include "kernel/irq.c"



/* Test cases */
static void test_int_flag(void) {
    /* Tested function call */
    bool_t _result = int_flag();
    CU_ASSERT_EQUAL(_result, FALSE);
}



static void test_int_flag_set(void) {
    bool_t _result;

    /* Tested function call */
    int_flag_set();

    _result = int_flag();
    CU_ASSERT_EQUAL(_result, TRUE);
}



static void test_int_flag_clr(void) {
    bool_t _result;

    int_flag_set();

    /* Tested function call */
    int_flag_clr();

    _result = int_flag();
    CU_ASSERT_EQUAL(_result, FALSE);
}



static void test_int_nesting(void) {
    /* Tested function call */
    uword_t _result = int_nesting();
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_int_nesting_sub(void) {
    uword_t _result;

    /* Tested function call */
    int_nesting_sub();
    _result = int_nesting();
    CU_ASSERT_EQUAL(_result, 0);
}



static void test_isr_install_0(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = 0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 0;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = 0;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    CU_ASSERT_PTR_EQUAL(israrry[_IntNum].link.next, &israrry[_IntNum].link);
}



static void test_isr_install_1(void) {
    int i;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum  = ~0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 0;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = 0;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    for(i=0; i<ISR_NUM; i++) {
        CU_ASSERT_PTR_EQUAL(israrry[i].link.next, &israrry[i].link);
    }
}



static void test_isr_install_2(void) {
    int i;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ~0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl )(void) = 0;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    for(i=0; i<ISR_NUM; i++) {
        CU_ASSERT_PTR_EQUAL(israrry[i].link.next, &israrry[i].link);
    }
}



static void irq_handler(void)
{
}



static void test_isr_install_3(void) {
    int i;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ~0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = irq_handler;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    for(i=0; i<ISR_NUM; i++) {
        CU_ASSERT_PTR_EQUAL(israrry[i].link.next, &israrry[i].link);
    }
}



static void test_isr_install_4(void) {
    isr_t *isr = NULL;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = 0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = irq_handler;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    isr = mac_find_entry(israrry[_IntNum].link.next, isr_t, link);
    CU_ASSERT_PTR_EQUAL(isr->pisr, irq_handler);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 1);
}



static void test_isr_install_5(void) {
    int i;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = irq_handler;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    for(i=1; i<ISR_NUM; i++) {
        CU_ASSERT_PTR_EQUAL(israrry[i].link.next, &israrry[i].link);
    }
}



static void test_isr_install_6(void) {
    isr_t *isr = NULL;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM - 1;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = irq_handler;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    isr = mac_find_entry(israrry[_IntNum].link.next, isr_t, link);
    CU_ASSERT_PTR_EQUAL(isr->pisr, irq_handler);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 1);
}


static void test_isr_install_7(void) {
    isr_t *isr = NULL;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM - 1;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = irq_handler;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    isr = mac_find_entry(israrry[_IntNum].link.next, isr_t, link);
    CU_ASSERT_PTR_EQUAL(isr->pisr, irq_handler);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 1);
}



static void test_isr_install_8(void) {
    isr_t *isr = NULL;

    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM - 1;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 2u;

    /* Initializing argument 3 (phdl) */
    void (*_phdl)(void) = irq_handler;

    /* Tested function call */
    isr_install(_IntNum, _IrqNo, _phdl);

    isr = mac_find_entry(israrry[_IntNum].link.next->next, isr_t, link);
    CU_ASSERT_PTR_EQUAL(isr->pisr, irq_handler);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 2);
}



static void test_isr_uninstall_0(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = 0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 0;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 1);
}



static void test_isr_uninstall_1(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ~0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 0;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
}


static void test_isr_uninstall_2(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 0;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
}



static void test_isr_uninstall_3(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = 1u;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 0;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
printf(".........................%d \n", israrry[_IntNum].nr);
    CU_ASSERT_PTR_EQUAL(israrry[_IntNum].link.next, &israrry[_IntNum].link);
}



static void test_isr_uninstall_4(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = 0;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
    CU_ASSERT_PTR_EQUAL(israrry[_IntNum].link.next, &israrry[_IntNum].link);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 0);
}



static void test_isr_uninstall_5(void) {
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM - 1;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 1u;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 1);
}



static void test_isr_uninstall_6(void)
{
    /* Initializing argument 1 (IntNum) */
    uword_t _IntNum = ISR_NUM - 1;

    /* Initializing argument 2 (IrqNo) */
    uword_t _IrqNo = 2u;

    /* Tested function call */
    isr_uninstall(_IntNum, _IrqNo);
    CU_ASSERT_PTR_EQUAL(israrry[_IntNum].link.next, &israrry[_IntNum].link);
    CU_ASSERT_EQUAL(israrry[_IntNum].nr, 0);
}



static void test_isrHandler(void) {
    /* Tested function call */
    isrHandler();
}



int test_irq_init(void) {
    printf("Interruption management test ...\n\n");
    initirq();
    return(0);
}



int test_irq_clean(void) {
    printf("Interruption management test is over!\n\n");
    return(0);
}



CU_TestInfo test_irq_cases[] = {
    {"test_int_flag", test_int_flag},
    {"test_int_flag_clr", test_int_flag_clr},
    {"test_int_flag_set", test_int_flag_set},
    {"test_int_nesting", test_int_nesting},
    {"test_int_nesting_sub", test_int_nesting_sub},
    {"test_isr_install_0", test_isr_install_0},
    {"test_isr_install_1", test_isr_install_1},
    {"test_isr_install_2", test_isr_install_2},
    {"test_isr_install_3", test_isr_install_3},
    {"test_isr_install_4", test_isr_install_4},
    {"test_isr_install_5", test_isr_install_5},
    {"test_isr_install_6", test_isr_install_6},
    {"test_isr_install_7", test_isr_install_7},
    {"test_isr_install_8", test_isr_install_8},
    {"test_isr_uninstall_0", test_isr_uninstall_0},
    {"test_isr_uninstall_1", test_isr_uninstall_1},
    {"test_isr_uninstall_2", test_isr_uninstall_2},
    {"test_isr_uninstall_3", test_isr_uninstall_3},
    {"test_isr_uninstall_4", test_isr_uninstall_4},
    {"test_isr_uninstall_5", test_isr_uninstall_5},
    {"test_isr_uninstall_6", test_isr_uninstall_6},
    {"test_isrHandler", test_isrHandler},
    CU_TEST_INFO_NULL
};
